package com.dm.wallpaper.library.helpers;


import com.dm.wallpaper.library.utils.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class TimeHelper {
    public static final String FORMAT_MINUTE_SECOND = "%02d:%02d";

    public TimeHelper() {
    }

    public static int minuteToMilli(int minute) {
        return minute * 60 * 1000;
    }

    public static int milliToMinute(int milli) {
        return milli / 60 / 1000;
    }

    public static String getShortDateTime() {
        SimpleDateFormat dateFormat = getDefaultShortDateTimeFormat();
        Date date = new Date();
        return dateFormat.format(date);
    }

    public static String getLongDateTime() {
        SimpleDateFormat dateFormat = getDefaultLongDateTimeFormat();
        Date date = new Date();
        return dateFormat.format(date);
    }

    public static String getDateTime( SimpleDateFormat simpleDateFormat) {
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public static String getDisplayDateTime(String datetime,  SimpleDateFormat simpleDateFormat) {
        return getDisplayDate(datetime, simpleDateFormat) + ", " + getDisplayTime(datetime, simpleDateFormat);
    }

    public static String getDisplayDate(String datetime,  SimpleDateFormat simpleDateFormat) {
        Date date = parse(simpleDateFormat, datetime);
        SimpleDateFormat formatter = new SimpleDateFormat(getDefaultDisplayDateFormat(), Locale.getDefault());
        return formatter.format(date);
    }

    public static String getDisplayTime(String datetime,  SimpleDateFormat simpleDateFormat) {
        Date date = parse(simpleDateFormat, datetime);
        SimpleDateFormat formatter = new SimpleDateFormat(getDefaultDisplayTimeFormat(), Locale.getDefault());
        return formatter.format(date).replace("am", "AM").replace("pm", "PM");
    }


    public static Date parse(SimpleDateFormat simpleDateFormat, String datetime) {
        try {
            return simpleDateFormat.parse(datetime);
        } catch (Exception var3) {
            Log.e("TimeHelper.parse([simpleDateFormat, datetime]):63  "+var3.getMessage());
            return null;
        }
    }

    private static String getDefaultDisplayDateFormat() {
        return "MMM dd yyyy";
    }

    private static String getDefaultDisplayTimeFormat() {
        return "hh:mm a";
    }

    public static SimpleDateFormat getDefaultLongDateTimeFormat() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault());
    }

    public static SimpleDateFormat getDefaultShortDateTimeFormat() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    public static String getFormattedTime(String format, int millis) {
        return String.format(Locale.getDefault(), format, TimeUnit.MILLISECONDS.toMinutes((long)millis), TimeUnit.MILLISECONDS.toSeconds((long)millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)millis)));
    }
}